package main.java.web.servlet;

import main.java.domain.Task;
import main.java.domain.User;
import main.java.service.UserService;
import main.java.service.impl.TaskServiceImpl;
import main.java.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/detailServlet")
public class DetailServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求参数
        String taskID = request.getParameter("task_id");

        HttpSession session = request.getSession();

        // 根据作业id找到作业
        TaskServiceImpl service = new TaskServiceImpl();
        Task task = service.find(Integer.parseInt(taskID));
        session.setAttribute("task", task);
        String[] files = task.getFilepath().split(";");

        session.setAttribute("files", files);

        // 查找当前学生所属老师
        UserService userService = new UserServiceImpl();
        User teacher = userService.findTeacherOfTaskID(Integer.parseInt(taskID));
        session.setAttribute("teacher", teacher);

        request.getRequestDispatcher("/detail.jsp").forward(request, response);
    }
}
